<?
include_once("CMBGenericWebService.php");
$service = new CMBGenericWebService("db28cm", "hmlb001", "db2adm");
$result = $service->search('/LIB01');
print_r($result);
$i=0;
$no_results=false;
function thumbnail($service,$URI,$local){
	$url = "thumbnails/".$local."-thumbnail.jpg";
	if(!file_exists($url)){
		$src_img = imagecreatefromstring($service->getItemData($URI));
		if($src_img){
			$old_x=imageSX($src_img);
			$old_y=imageSY($src_img);
			if ($old_x > $old_y) {
				$thumb_w=100;
				$thumb_h=$old_y*(100/$old_x);
			}
			if ($old_x < $old_y) {
				$thumb_w=$old_x*(100/$old_y);
				$thumb_h=100;
			}
			if ($old_x == $old_y) {
				$thumb_w=100;
				$thumb_h=100;
			}
			$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
			imagejpeg($dst_img, $url, 100);
			imagejpeg($dst_img, NULL, 100);
			exit;
		}
	}else {
		$img = imagecreatefromjpeg($url);
		imagejpeg($img, NULL, 100);
		exit;
	}
	return false;
}
function html_headers_results($items){
	$mappings_text = array( 
		"LIBLocal" => "Local",
		"LIBBox" => "Box",
		"LIBFile" => "File",
		"LIBIndex" => "Index",
		"LIBDate" => "Date",
		"LIBSize" => "Size",
		"LIBTitle" => "Title",
	);
	$i=0;
	$mappings = array();
	foreach($mappings_text as $key => $value){
		$mappings[$key] = array( $value, $i);
		$i++;
	}
	$i=0;
	foreach($items as $item){
		$URIs[$i] = $item->attributes()->URI;
		foreach($item->children()->children()->attributes() as $attr =>  $value){
			$myData[$i][$attr] = $value;
		}
		$i++;
	}
?> </script>
	<script type="text/javascript">
		var data = new google.visualization.DataTable();
		var table = new google.visualization.Table();
		var options = {'showRowNumber': true , 'page': 'enable', 'pagingButtonsConfiguration': 'auto'};
		var gallery = new Array(<?php sizeof($myData) ?>);
<?php
			foreach($mappings as $a ) {
				echo '
			data.addColumn(\'string\', "'.$a[0].'");';
			}
			echo '
			data.addRows('.sizeof($myData).');';

			$i = 0;
			foreach($myData as $item ){
				foreach($item as $attr =>  $value ){
					if( isset($mappings["$attr"][1] ) ){
						echo '
			data.setCell('.$i.', '.$mappings["$attr"][1].',"'.$value.'");';
					}
				}
				echo '
			gallery['.$i.'] = [ "?URI='.$URIs[$i].'", "('.$item["LIBLocal"].") ".$item["LIBTitle"].'" ];
			data.setRowProperty('.$i.', \'URI\', "'.urlencode($URIs[$i]).'");
					';
				$i++;
			}
		?>

		function drawThumbnails() {
			var row;
			var myWidth = 1024;
			var thumb_table = document.createElement("table");
			thumb_table.setAttribute("id" ,"thumb_table");
			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				myWidth = window.innerWidth;
			} else if( document.documentElement && document.documentElement.clientWidth ) {
				//IE 6+ in 'standards compliant mode'
				myWidth = document.documentElement.clientWidth;
			} else if( document.body && document.body.clientWidth ) {
				//IE 4 compatible
				myWidth = document.body.clientWidth;
			}
			var count = parseInt(myWidth / 200-1);
			if( count < 1 ){ count = 1; }
			for(var i =0;i < data.getNumberOfRows();i++){
				rowID = "row"+String(parseInt(i/count));
				if(i%count == 0) { 
					row = document.createElement("tr");
					row.setAttribute("id", rowID);
					thumb_table.appendChild(row);
				}
				result = document.createElement("td");
				result.setAttribute("align", "center");
				var local = data.getValue(i,0);
				var title = data.getValue(i,6);
				var URI = data.getRowProperty(i, "URI");
				result.innerHTML = '<table class="thumbs" width=200><tbody><tr><td colspan=2 style="text-align: center"><a onClick="thumbSelectHandler('+i+');return 0;" rel="lightbox-results"> <img style="text-align: center" src="?local='+local+'&Thumbnail='+URI+'"></a> <tr><td style="text-align:right">Local:</td><td style="text-align:left">'+local+'</td></tr> <tr><td style="text-align:right">Box:</td><td style="text-align:left">'+data.getValue(i,1)+'</td></tr> <tr><td style="text-align:right">File:</td><td style="text-align:left">'+data.getValue(i,2)+'</td></tr> <tr><td style="text-align:right">Index:</td><td style="text-align:left">'+data.getValue(i,3)+'</td></tr> <tr><td style="text-align:right">Date:</td><td style="text-align:left">'+data.getValue(i,4)+'</td></tr> <tr><td style="text-align:right">Size:</td><td style="text-align:left">'+data.getValue(i,5)+'</td></tr> <tr><td colspan="2">'+title+'</td></tr></tbody></table>';

				row.appendChild(result);
			}
			document.getElementById('results').innerHTML = '';
			document.getElementById('results').innerHTML = "<table>"+thumb_table.innerHTML+"</table>";
		}
		function drawTable() { 
			table = new google.visualization.Table(document.getElementById('results'));
			options['pageSize'] = 10;
			for(var i=0;i<document.form1.page_size.length;i++){
				if(document.form1.page_size[i].checked){
					options['pageSize'] = (String)(document.form1.page_size[i].value);
					options['pageSize'] = 10;
				}

			}
				
			google.visualization.events.addListener(table, 'select', selectHandler);
			google.visualization.events.addListener(table, 'sort', sortHandler);
			table.draw(data, options);
		}
		function sortHandler(event){
			data.sort([{column: event.column, desc: !event.ascending}]);
			var tmp = new Array(gallery.length);
			for(var i = 0; i < tmp.length;i++){
				tmp[i] = ["?URI="+data.getRowProperty(i, 'URI'), "("+data.getValue(i,0)+") "+data.getValue(i,6)]
			}
			gallery = tmp;
			options['sortColumn'] = event.column;
			options['sortAscending'] = event.ascending;
			table.draw(data,options);
			table.setSelection();
		}
		function selectHandler() { jQuery.slimbox( gallery, table.getSelection()[0].row ); }
		function thumbSelectHandler(id) { jQuery.slimbox( gallery, id ); }
		function drawDisplay(){
			if(document.form1.result_view[0].checked){
				drawThumbnails();
			} else {
				drawTable();
			}
		}
		function resizeTable(){
			if(document.form1.result_view[0].checked){
            drawThumbnails();
         }
		}
		$(window).bind('resize', resizeTable);
		google.setOnLoadCallback(drawDisplay);
<?php
}
function html_headers(){
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link rel="stylesheet" type="text/css" href="css/style.css" />
		<link rel="stylesheet" href="css/slimbox2.css" type="text/css" media="screen" />
		<SCRIPT language="Javascript">
			if (document.images) {
				homeOff = new Image
				homeOff.src = 'http://library.marist.edu/archives/images/homeOff.gif'
				maristOff = new Image
				maristOff.src = 'http://library.marist.edu/archives/images/maristOff.gif'
				libraryOff = new Image
				libraryOff.src = 'http://library.marist.edu/archives/images/libraryOff.gif'

				homeOn = new Image
				homeOn.src = 'http://library.marist.edu/archives/images/homeOn.gif'
				maristOn = new Image
				maristOn.src = 'http://library.marist.edu/archives/images/maristOn.gif'
				libraryOn = new Image
				libraryOn.src = 'http://library.marist.edu/archives/images/libraryOn.gif'
			}

			else {
				home = ""    ;  homeON = ""    ;  document.home = ""
				marist = ""   ;  maristON = ""   ;  document.marist = ""
				library = ""  ;  libraryON = ""  ;  document.library = ""
			}
		</script>
		<script type="text/javascript" src="http://www.google.com/jsapi"></script>
		<script type="text/javascript">
			google.load('visualization', '1', {packages: ['table']});
			google.load('jquery', '1');
			function compileSearchString() {
				var box,file,index,title;
				var search_string = "/LIB01";
//				if(!(document.form1.local.value == "*" || document.form1.local.value == "" )){
//					search_string += "[@LIBLocal LIKE \"%" + document.form1.local.value + "%\"]";
//				}
				if(!(document.form1.title.value == "*" || document.form1.title.value == "" )){
					search_string += "[@LIBTitle LIKE \"%" + document.form1.title.value + "%\"]";
				}
				search_string = search_string.replace('*','%','g');
				document.form1.value.value = search_string;
			}
<?php
};
function html_body(){
?>

		</script>
		<script type="text/javascript" src="js/slimbox2.js"></script></head><body>
		<!-- ***** Main table begins here *****-->
<div id="bodyWrapper"><div id="header"><div id="logo"></div></div><div id="menu"><div id="menuItems"><a href="http://www.marist.edu">Marist Home</a><a href="http://library.marist.edu/">Marist Library</a><a target="_blank" href="http://library.marist.edu/archives/index.html">Home</a><a target="" href="http://library.marist.edu/archives/LTP/LTP.xml">LT Papers</a></div></div><div id="contentWrapper"><div id="bodyContent"><title>LTC Content Manager Demo</title>
		  <!-- ***** Side table ends here *****-->

		<!--***** CONTENT GOES HERE *****-->
		<h2 style="text-align: center"> Lowell Thomas Papers<br />
		 Graphic Materials Series</h2>

<?php
};
function html_footer(){
?>
	</body>
</html>
<?php
};
function html_form(){
?>
		<form method="post" name="form1" onSubmit="compileSearchString();return 0">
			<table style="text-align: right;margin-left: auto;margin-right: auto">
			<input type="hidden" name="action" value="search" />
			<input type="hidden" name="value" value="/LIB01" />
<?php /*
			<tr><td>Local:</td><td> <input type="text" name="local" value="<?php if(isset($_POST["local"])) echo $_POST["local"]; else echo '"*"'; ?>" /><BR /></td></tr>
			<tr><td>Box:</td><td><input type="text" name="box" value="<?php if(isset($_POST["box"])) echo $_POST["box"]; else echo '"*"'; ?>" /></><BR /></td></tr>
			<tr><td>File:</td><td> <input type="text" name="file" value="<?php if(isset($_POST["file"])) echo $_POST["file"]; else echo '"*"'; ?>"  /><BR /></td></tr>
			<tr><td>Index:</td><td> <input type="text" name="index" value="<?php if(isset($_POST["index"])) echo $_POST["index"]; else echo '"*"'; ?>" /><BR /></td></tr>
*/?>
			<tr><td>Keyword:</td><td> <input type="text" name="title" value="<?php if(isset($_POST["title"])) echo $_POST["title"]; else echo '"*"'; ?>" /><BR /></td></tr>
			<br/>
			<tr><td>Results Per Page:</td><td><input type="radio" <?php
if(isset($_POST["result_view"])) echo 'onClick="drawDisplay();return 0;"';
?> name="page_size" id="page_size" value="10" <? if($_POST["page_size"] == "10") echo "checked";?> checked>10<input type="radio" <?php
if(isset($_POST["result_view"])) echo 'onClick="drawDisplay();return 0;"';
?> name="page_size" id="page_size" value="25" <? if($_POST["page_size"] == "25") echo "checked";?> disabled>25<input type="radio" <?php
if(isset($_POST["result_view"])) echo 'onClick="drawDisplay();return 0;"';
?> name="page_size" id="page_size" value="50" <? if($_POST["page_size"] == "50") echo "checked";?> disabled>50</td></tr>
			<tr><td>View:</td><td><input type="radio" <?php
if(isset($_POST["result_view"])) echo 'onClick="drawThumbnails();return 0;"';
?> name="result_view" value="Thumbnail" <? if($_POST["result_view"] == "Thumbnail") echo "checked";?>>Gallery<input type="radio" <?php
if(isset($_POST["result_view"])) echo 'onClick="drawTable();return 0;"';
?> name="result_view" value="Table" <? if($_POST["result_view"] != "Thumbnail") echo "checked";?>>Table</td></tr>
<tr><td colspan="2" style="text-align: center"><input type="submit" value="Search" onClick="compileSearchString();return 0" /></td></tr>
</table>
		</form>
	<div id="results"></div>
<?php
};
/*
 * No changes should need to be made
 * below this comment.
 *
 */
if(isset($_GET['URI']))
	$service->getItem($_GET["URI"]) and exit;
if(isset($_GET['Thumbnail']) && isset($_GET['local']))
	thumbnail($service, $_GET['Thumbnail'], $_GET['local'] ) and exit;
if(!isset($_POST["action"]) || !($_POST["action"] == "display") ){
	html_headers();
}
if(isset($_POST['action'])) {
	if ($_POST['action'] == "display" ){
		try{
			$service->getItem($_POST["URI"]) and exit;
		}
		catch( SoapFault $fault) {
         echo "REQUEST:\n" . $service->getLastRequest() . "\n";
         print("SOAP Fault: (faultcode: {$fault->faultcode}\n" .
            "faultstring: {$fault->faultstring})");
      }
	} else if($_POST['action']=="search") {
		try{
			$result = $service->search($_POST["value"]);
			$xml = simplexml_load_string($result->processXMLRequestReturn->xmlResponseText);
			unset($result);
			if($xml->ResultSet->attributes()->count > 0){
				foreach( $xml->ResultSet->Item as $item ){
					$items[] = $item;
				}
				unset($xml);
				html_headers_results($items);
			}
			else {
				$no_results = TRUE;
			}
			html_body();
			if($no_results){
?>
		<h3 style="text-align: center;">No Results Found</h3>
<?php
			}
		} 
		catch( SoapFault $fault) {
			echo "REQUEST:\n" . $service->getLastRequest() . "\n";
			print("SOAP Fault: (faultcode: {$fault->faultcode}\n" .
				"faultstring: {$fault->faultstring})");
		}
	}
}else { 
	html_body();
}
html_form();
html_footer();
// vim: ts=3:sw=3
?>
